﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using WCFAlmacen.Dominio;
using System.Data.SqlClient;

namespace WCFAlmacen.Persistencia
{
    public class GREstadoMatDAO
    {

        public List<GREstadoMat> ListarEstadosMateriales()
        {

            SqlDataReader rdr = null;
            SqlConnection con = null;
            SqlCommand cmd = null;
            List<GREstadoMat> ListaEstadosMat = null;
            GREstadoMat objEstadoMaterial = null;

            try
            {
                con = new SqlConnection(ConexionUtil.ObtenerCadena());
                con.Open();
                cmd = new SqlCommand("GREstadoMat_Listar", con);
                cmd.CommandType = CommandType.StoredProcedure;
                rdr = cmd.ExecuteReader();

                if (rdr.HasRows)
                {
                    ListaEstadosMat = new List<GREstadoMat>();
                    while (rdr.Read())
                    {
                        objEstadoMaterial = new GREstadoMat();
                        objEstadoMaterial.iCodEstMat = Convert.ToInt32(rdr["iCodEstMat"].ToString());
                        objEstadoMaterial.vNomEstMat = rdr["vNomEstMat"].ToString();
                        objEstadoMaterial.iCodUsu = Convert.ToInt32(rdr["iCodUsu"].ToString());
                        objEstadoMaterial.vNomTer = rdr["vNomTer"].ToString();
                        objEstadoMaterial.dtFecAct = Convert.ToDateTime(rdr["dtFecAct"].ToString());
                        ListaEstadosMat.Add(objEstadoMaterial);
                    }
                }
            }
            catch
            {
                ListaEstadosMat = null;
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();

                if (con.State == ConnectionState.Open)
                    con.Close();
            }
            return ListaEstadosMat;
        }

        public GREstadoMat ObtenerDatosEstadoMaterial(Int32 intCodEstadoMaterial)
        {

            SqlDataReader rdr = null;
            SqlConnection con = null;
            SqlCommand cmd = null;
            GREstadoMat objEstadoMaterial = null;

            try
            {
                con = new SqlConnection(ConexionUtil.ObtenerCadena());
                con.Open();
                cmd = new SqlCommand("GREstadoMat_Obtener", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@iCodEstMat", SqlDbType.Int).Value = intCodEstadoMaterial;
                rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        objEstadoMaterial = new GREstadoMat();
                        objEstadoMaterial.iCodEstMat = Convert.ToInt32(rdr["iCodEstMat"].ToString());
                        objEstadoMaterial.vNomEstMat = rdr["vNomEstMat"].ToString();
                        objEstadoMaterial.iCodUsu = Convert.ToInt32(rdr["iCodUsu"].ToString());
                        objEstadoMaterial.vNomTer = rdr["vNomTer"].ToString();
                        objEstadoMaterial.dtFecAct = Convert.ToDateTime(rdr["dtFecAct"].ToString());
                    }
                }
            }
            catch
            {
                objEstadoMaterial = null;
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();

                if (con.State == ConnectionState.Open)
                    con.Close();
            }
            return objEstadoMaterial;
        }
    }
}